package com.stevex.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MainController {
	Logger logger = LoggerFactory.getLogger(MainController.class);
	
	@RequestMapping(value = "/hello", method = RequestMethod.GET, produces="text/plain")
	@ResponseBody
	public String hello() {
		logger.info("request caught in hello method.");
		
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		UserDetails user = (UserDetails)auth.getPrincipal();
		
		return "Hello Operator " + user.getUsername();
	}
	
	@RequestMapping(value = "/admin", method = RequestMethod.GET, produces="text/plain")
	@ResponseBody
	public String admin() {
		logger.info("request caught in admin method.");
		
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		UserDetails user = (UserDetails)auth.getPrincipal();
		
		return "Hello Administrator " + user.getUsername();
	}
}
